package com.to8to.kitt.esb;

import java.util.Iterator;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.elasticsearch.common.base.Joiner;

public class ZkConfig {
	
	Configuration config;
	
	public String zkhosts;
	public int sessionTimeoutMs;
	public int connectionTimeoutMs;
	public int baseSleepTimeMs;
	public int maxRetries;
	public String servicePath;
	public String configPath;
	
	public ZkConfig(String file)
	{
		try {
			config = new PropertiesConfiguration(file);
			load();
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public ZkConfig(Configuration conf)
	{
		config = conf;
		load();
	}
	
	
	public void load()
	{
		String[] array = config.getStringArray("zk.hosts");
		this.zkhosts = Joiner.on(",").join(array);
		this.sessionTimeoutMs = config.getInt("zk.sessionTimeoutTM", 3000);
		this.connectionTimeoutMs = config.getInt("zk.connectionTimeoutTM", 2000);
		this.baseSleepTimeMs = config.getInt("zk.baseSleepTimeMs", 1000);
		this.maxRetries = config.getInt("zk.maxRetries", 10);
		this.servicePath = config.getString("zk.path.service");
		this.configPath = config.getString("zk.path.config");
	}
	
	public String toString()
	{
		StringBuilder sb = new StringBuilder();
		Iterator<String> it = config.getKeys();
		while(it.hasNext())
		{
			String k = it.next();
			
			Object v = config.getProperty(k);
			
			sb.append(k + "=" + v + "\n");
		}
		return sb.toString();
	}
}
